var MANDATORY_VALUE_AXIS_COUNT = 1;
var PIE_VALUE_AXIS_COUNT = 1;
var SUMMARY_VALUE_EXPANDO = "SummaryValue=\"{0}\"";
var VALUE_AXIS_COUNT = 2;

var _oAttributeInfoCache = null;
var _oChartLayoutXmlBuilder = null;
var _iColumnAxisType = null;
var _saGroupingIntervalFormats = null;
var _saSummaryValueFormats = null;
var _oWizardXmlBuilder = null;





function chartFormatPage_onload()
{

initializeChartingCommon();


previewImage.src = formatString("/_imgs/ReportWizard/chart_{0}.gif", _oChartLayoutXmlBuilder.GetChartType().toLowerCase());


_saGroupingIntervalFormats = getGroupIntervalArray();
_saSummaryValueFormats = getSummaryValueArray();


initializeColumnAxisSelect();


initializeValueAxisSelects();



updatePreviewAxes();


updateNextButton();


showDataLabels.checked = _oChartLayoutXmlBuilder.GetShowDataLabels()
showLegend.checked = _oChartLayoutXmlBuilder.GetShowLegend()
}




function getLabelControl(oFieldControl)
{
var oLabelControl = null;
switch (oFieldControl.id)
{
case "columnAxisField":
oLabelControl = columnAxisLabel;
break;
case "valueAxis1Field":
oLabelControl = valueAxis1Label;
break;
case "valueAxis2Field":
oLabelControl = valueAxis2Label;
break;
}

return oLabelControl;
}





function getValueAxes()
{

var oaValueAxes = new Array();


var oaColumns = _oWizardXmlBuilder.GetColumns();


for (var i = 0; i < oaColumns.length; i++)
{

var oColumn = oaColumns[i];
var sColumnId = oColumn.getAttribute("ID");
var sAttributeAlias = oColumn.getAttribute("Field");
var sSummaryValue = oColumn.getAttribute("SummaryValue");
var sDisplayName = _oAttributeInfoCache.GetAttributeDisplayName(sAttributeAlias);

var oValueAxis = null;
if (_iColumnAxisType == PROPERTY_PAGE_TYPE_GROUP)
{


if (!IsNull(sSummaryValue))
{
oValueAxis = new ValueAxis(sColumnId, sDisplayName, sSummaryValue);
}
}
else
{


if (isNumericDataType(_oAttributeInfoCache.GetAttributeType(sAttributeAlias)))
{
oValueAxis = new ValueAxis(sColumnId, sDisplayName, sSummaryValue);
}
}

if (!IsNull(oValueAxis))
{
oaValueAxes.push(oValueAxis);
}
}

return oaValueAxes;
}




function getValueAxisSelect(oaValueAxes, bIncludeEmptyOption)
{

var oValueAxisSelect = new Select();


if (bIncludeEmptyOption)
{
oValueAxisSelect.AddEmptyOption();
}


var oaColumns = _oWizardXmlBuilder.GetColumns();


for (var i = 0; i < oaValueAxes.length; i++)
{

var oValueAxis = oaValueAxes[i];





if (_iColumnAxisType == PROPERTY_PAGE_TYPE_COLUMN || (_oChartLayoutXmlBuilder.GetChartType() != CHART_TYPE_PIE || isValidForPieChart(oValueAxis)))
{


var sOptionDisplayName = (_iColumnAxisType == PROPERTY_PAGE_TYPE_GROUP) ? formatString(_saSummaryValueFormats[oValueAxis.SummaryValue], oValueAxis.Name) : oValueAxis.Name;


oValueAxisSelect.AddOption(sOptionDisplayName, oValueAxis.Id, formatString(SUMMARY_VALUE_EXPANDO, oValueAxis.SummaryValue));
}
}

return oValueAxisSelect;
}




function initializeColumnAxisSelect()
{
var oColumnAxisSelect = new Select();
oColumnAxisSelect.ID = "columnAxisField";
oColumnAxisSelect.OnChange = "updateLabel(this);updateNextButton();";


var oColumnAxisNode = _oChartLayoutXmlBuilder.GetColumnAxis();
if (!IsNull(oColumnAxisNode))
{


oColumnAxisSelect.Selected = oColumnAxisNode.getAttribute("Field");
}

if (_iColumnAxisType == PROPERTY_PAGE_TYPE_GROUP)
{

var oaGroupings = _oWizardXmlBuilder.GetGroupings();




for (var i = 0; i < oaGroupings.length; i++)
{



var oGrouping = oaGroupings[i];
var sAttributeAlias = oGrouping.getAttribute("Field");
var sDisplayNameBase = _oAttributeInfoCache.GetAttributeDisplayName(sAttributeAlias);


var sTimeInterval = oGrouping.getAttribute("Interval");
var sDisplayName = (IsNull(sTimeInterval)) ? sDisplayNameBase : formatString(_saGroupingIntervalFormats[sTimeInterval], sDisplayNameBase);


oColumnAxisSelect.AddOption(sDisplayName, sAttributeAlias);
}
}
else
{

var oaColumns = _oWizardXmlBuilder.GetColumns();




for (var i = 0; i < oaColumns.length; i++)
{


var oColumn = oaColumns[i];
var sAttributeAlias = oColumn.getAttribute("Field");
var sDisplayName = _oAttributeInfoCache.GetAttributeDisplayName(sAttributeAlias);


oColumnAxisSelect.AddOption(sDisplayName, oColumn.getAttribute("ID"));
}
}


columnAxisField.parentNode.innerHTML = oColumnAxisSelect.Render();


if (!IsNull(oColumnAxisNode) && columnAxisField.DataValue == oColumnAxisSelect.Selected)
{

columnAxisLabel.DataValue = oColumnAxisNode.getAttribute("Name");
}
else
{


updateLabel(columnAxisField);
}
}




function initializeChartingCommon()
{


_oWizardXmlBuilder = new WizardXmlBuilder();
_oWizardXmlBuilder.SetGroupingXml(WizardGetProperty("GroupingXml"));
_oWizardXmlBuilder.SetColumnXml(WizardGetProperty("ColumnXml"));
_oWizardXmlBuilder.SetTableLayoutXml(WizardGetProperty("TableLayoutXml"));


_iColumnAxisType = (_oWizardXmlBuilder.GetGroupingCount() > 0) ? PROPERTY_PAGE_TYPE_GROUP : PROPERTY_PAGE_TYPE_COLUMN;


_oAttributeInfoCache = new AttributeInfoCache();
_oAttributeInfoCache.Initialize(WizardGetProperty("FetchXml").xml, WizardGetProperty("Language"));


_oChartLayoutXmlBuilder = new ChartLayoutXmlBuilder();
_oChartLayoutXmlBuilder.SetLayoutXml(WizardGetProperty("ChartLayoutXml"));
}




function initializeValueAxisSelects()
{




var bColumnAxisTypeChanged;
var oOriginalColumnAxis = _oChartLayoutXmlBuilder.GetColumnAxis();
if (!IsNull(oOriginalColumnAxis))
{
var sOriginalColumnAxisField = oOriginalColumnAxis.getAttribute("Field");
if (_iColumnAxisType == PROPERTY_PAGE_TYPE_GROUP)
{
bColumnAxisTypeChanged = IsNull(_oWizardXmlBuilder.GetGrouping(sOriginalColumnAxisField));
}
else
{
bColumnAxisTypeChanged = IsNull(_oWizardXmlBuilder.GetColumn(sOriginalColumnAxisField));
}
}
else
{
bColumnAxisTypeChanged = true;
}


var oaValueAxes = getValueAxes();



var iValueAxisCount = (_oChartLayoutXmlBuilder.GetChartType() == CHART_TYPE_PIE) ? PIE_VALUE_AXIS_COUNT : VALUE_AXIS_COUNT;
for (var i = 0; i < iValueAxisCount; i++)
{
var bOptionalAxis = (i >= MANDATORY_VALUE_AXIS_COUNT);


var oValueAxisSelect = getValueAxisSelect(oaValueAxes, bOptionalAxis);
oValueAxisSelect.ID = formatString("valueAxis{0}Field", i + 1);
oValueAxisSelect.OnChange = (bOptionalAxis) ? "updateLabel(this);updateOptionalValueAxisLabel(this);updateNextButton();" : "updateLabel(this);updateNextButton();";



var oValueAxisNode = _oChartLayoutXmlBuilder.GetValueAxis(i);
if (!IsNull(oValueAxisNode))
{
oValueAxisSelect.Selected = oValueAxisNode.getAttribute("Field");
}


var oFieldControl = document.all[oValueAxisSelect.ID];
oFieldControl.parentNode.innerHTML = oValueAxisSelect.Render();



var oFieldControl = document.all[oValueAxisSelect.ID]
if (IsNull(oValueAxisNode) || bColumnAxisTypeChanged || oFieldControl.DataValue != oValueAxisSelect.Selected)
{


updateLabel(oFieldControl);
}
else
{




var oColumnNode = _oWizardXmlBuilder.GetColumn(oValueAxisSelect.Selected);

if (_iColumnAxisType == PROPERTY_PAGE_TYPE_GROUP && oValueAxisNode.getAttribute("SummaryValue") != oColumnNode.getAttribute("SummaryValue"))
{


updateLabel(oFieldControl);
}
else
{

var oLabelControl = document.all[formatString("valueAxis{0}Label", i + 1)];
oLabelControl.DataValue = oValueAxisNode.getAttribute("Name");
}
}



if (bOptionalAxis)
{
updateOptionalValueAxisLabel(oFieldControl)
}
}
}





function isValidForPieChart(oValueAxis)
{
return (oValueAxis.SummaryValue == SUMMARY_VALUE_SUM || oValueAxis.SummaryValue == SUMMARY_VALUE_PERCENT_TOTAL);
}





function updateLabel(oFieldControl)
{

var oLabelControl = getLabelControl(oFieldControl);

if (!IsNull(oLabelControl))
{

oLabelControl.DataValue = oFieldControl.SelectedText;


updatePreviewAxes();
}
}





function updateOptionalValueAxisLabel(oFieldControl)
{
var oLabelControl = getLabelControl(oFieldControl);

if (IsNull(oFieldControl.DataValue))
{
oLabelControl.DataValue = null;
oLabelControl.Disabled = true;
}
else
{
oLabelControl.Disabled = false;
}
}






function updateNextButton()
{

var bIsPieChart = (_oChartLayoutXmlBuilder.GetChartType() == CHART_TYPE_PIE);

var bEnableNextButton =

!IsNull(columnAxisField.DataValue) && !IsNull(valueAxis1Field.DataValue) &&

(bIsPieChart || (!IsNull(columnAxisLabel.DataValue) && !IsNull(valueAxis1Label.DataValue))) &&


(bIsPieChart || IsNull(valueAxis2Field.DataValue) || !IsNull(valueAxis2Label.DataValue));

WizardSetButtonEnabled(bEnableNextButton, _WizardButtonsEnum.NEXTBUTTON);
}





function updatePreviewAxes()
{
var sChartType = _oChartLayoutXmlBuilder.GetChartType();
if (sChartType == CHART_TYPE_PIE)
{


xAxisCell.innerText = formatString(LOCID_PIE_CHART_PREVIEW, valueAxis1Field.SelectedText, columnAxisField.SelectedText);
}
else
{

var sColumnAxisLabel = Trim(columnAxisLabel.value);



var sValueAxisLabel = Trim(valueAxis1Label.value);
if (!IsNull(valueAxis2Field.DataValue))
{
sValueAxisLabel += LOCID_VALUE_AXIS_SEPERATOR;
sValueAxisLabel += Trim(valueAxis2Label.value);
}


switch (sChartType)
{
case CHART_TYPE_COLUMN:
case CHART_TYPE_LINE:
xAxisCell.innerText = sColumnAxisLabel;
yAxisCell.innerText = sValueAxisLabel;
break;
case CHART_TYPE_BAR:
xAxisCell.innerText = sValueAxisLabel;
yAxisCell.innerText = sColumnAxisLabel;
break;
}
}
}




function ValueAxis(sId, sName, sSummaryValue)
{
this.Id = sId;
this.Name = sName;




this.SummaryValue = IsNull(sSummaryValue) ? SUMMARY_VALUE_SUM : sSummaryValue;
}
